package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ${wushenglu} on 2017/7/27 0027.
 */
@Controller
@RequestMapping(ControllerConstants.MCITY)
public class CityMController extends BaseMobileController {
    @Autowired
    private CityService cityService;

    /**
     * 获取热门城市，城市列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getCity")
    public void getCity(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                int parentId = getInt(json, "parentId", 1);
                String key = getString(json, "key", "");
                return cityService.getCity(parentId, key);
            }
        });
    }

    /**
     * 获取城市
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getCityByParentId")
    public void getCityByParentId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                int parentId = getInt(json, "parentId", 1);
                return cityService.getCityByParentId(parentId);
            }
        });
    }

    /**
     * 获取区级编号
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getCityByLngLat")
    public void getCityByLngLat(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer level = getInt(json, "level", 4);//国家 ： 1  ， 省 ： 2   ，市：  3 ，区 4
                String lng = getString(json, "lng", "");
                String lat = getString(json, "lat", "");
                return cityService.getCityByLngLat(level, lng, lat);
            }
        });
    }
}
